﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Configuration;
using Wys.ClosePopup;
using System.Reflection;
namespace Wys.ClosePopup
{
    public partial class MainForm : Form
    {
       const uint BM_CLICK = 0xF5;

       private String _spitToken = ",";
        private int _intSpan = 100;
        private bool _quit = false;
        private bool _Hide = false;
        private ClosePopupConfig _cpc = new ClosePopupConfig();
        private List<ClosePopupCommon> _lstCommon = new List<ClosePopupCommon>();
        private List<String> _lstFindString = new List<string>();
       
        public MainForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            GUID1000.Text = DateTime.Now.ToString("yyyy年MM月dd日HH时mm分")+"开始运行";
            _spitToken = _cpc.GetToken();
            _intSpan = Convert.ToInt32(_cpc.GetTimeSpan());
            this.Text = _cpc.GetAppTitle();
            this.托盘.Text = _cpc.GetAppTitle();
            List<CClosePopupSection> _lst = _cpc.GetClosePopupSection();
            if (_lst.Count == 0)
            {
                CMessage.ShowMessage("配置文件不正确", _cpc.GetAppTitle());
                _quit = true;
                this.Close();
            }
            if (_intSpan < 50)
            {
                CMessage.ShowMessage("定时器的间隔不能小于50毫秒", _cpc.GetAppTitle());
                _quit = true;
                this.Close();
            }
            for (int i = 0; i < _lst.Count; i++)
            {
                IClosePopup ty=null;
                try
                {
                    ty = Assembly.LoadFrom(_lst[i].DllName).CreateInstance(_lst[i].ClassName) as IClosePopup;
                }
                catch (Exception)
                {
                    ty = new CommonClosePopup();
                }
                string[] strSpit = _lst[i].FindTitleString.Split(new string[] { _spitToken }, StringSplitOptions.RemoveEmptyEntries);
                for (int j = 0; j < strSpit.Length; j++)
                {
                    ClosePopupCommon cpc = new ClosePopupCommon();
                    cpc.csp = ty;
                    cpc.FindString = strSpit[j];
                    _lstFindString.Add(strSpit[j]);
                    _lstCommon.Add(cpc);
                }
            }         
            for (int i = 0; i < _lstCommon.Count; i++)
            {
               dg.Rows.Add(new String[] { _lstCommon[i].FindString, _lstCommon[i].csp.GetType().FullName , "0" });
               dg.Rows[i].HeaderCell.Value = (i+1).ToString();
               dg.Rows[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            }
            计时器.Interval = _intSpan;
            计时器.Enabled = true;
            托盘.Visible = true;
            this.Close();
        }

        private void scanWin()
        {
            for (int i = 0; i < _lstCommon.Count; i++)
            {
                dg.Rows[i].Cells["次数列"].Value  = _lstCommon[i].CloseCount.ToString();
            }
            int WinTitleLength = this._lstFindString.Count;
            String strMsg = "";
            for (int idex = 0; idex < WinTitleLength; idex++)
            {
                IntPtr hWnd = Win32.FindWindow(null, _lstFindString[idex]);
                if (hWnd != IntPtr.Zero)
                {
                    if (_lstCommon[idex].csp.CloseWindow(hWnd, ref strMsg))
                    {
                        _lstCommon[idex].CloseCount++;
                    }
                }                   
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            scanWin();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                计时器.Enabled = false;
                this.托盘.Visible = false;
                this.托盘.Dispose();
                _quit = true;
                Application.Exit();
            }
            catch (Exception)
            {
                
            }            
        }

        private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_Hide)
            {
                this.ShowInTaskbar = false;
                this.Visible = true;
                _Hide = false;
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.WindowsShutDown)
            {
                return;
            }
            if (!_quit)
            {
                this.ShowInTaskbar = false;
                this.Visible=false;
                _Hide = true;
                e.Cancel = true;
            }
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            if (_Hide)
            {
                this.ShowInTaskbar = false;
                this.Visible = true;
                _Hide = false;
            }
            else
            {
                this.ShowInTaskbar = false;
                this.Visible = false;
                _Hide = true;
            }  
        }

        public class ClosePopupCommon
        {
            public IClosePopup csp = null;
            public String FindString = "";
            public int CloseCount = 0;
        }

        private void 关于ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            using (AboutBox1 frm = new AboutBox1())
            {
                frm.ShowDialog();
            }
        }

        private void 退出ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            退出ToolStripMenuItem_Click(sender, e);
        }

        private void 显示ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            notifyIcon1_DoubleClick(sender, e);
        }
    }
}